package com.plz.afmt.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.plz.web.util.PageCommonUtil;
import com.plz.web.base.vo.PagerResult;
import com.plz.afmt.dto.TokenDto;
import com.plz.afmt.dto.TokenQueryDto;
import com.plz.afmt.entity.TokenEntity;
import com.plz.afmt.mapper.TokenMapper;
import com.plz.afmt.service.TokenService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plz.afmt.transfer.TokenTransfer;
import com.plz.afmt.vo.TokenVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 用户Token
 *
 * @author jzc
 */
@Service
public class TokenServiceImpl extends ServiceImpl<TokenMapper, TokenEntity> implements TokenService {
    @Autowired
    private TokenTransfer tokenTransfer;
    /**
     * 15天后过期
     */
    private final static int EXPIRE = 3600 * 24 * 30;

    @Override
    public PagerResult<TokenVo> pageQuery(TokenQueryDto dto) {
        QueryWrapper<TokenEntity> queryWrapper = PageCommonUtil.dtoToWrapper(TokenEntity.class, dto);
        queryWrapper.orderByDesc("id");
        PagerResult<TokenEntity> entityPagerResult = PageCommonUtil.sigelPage(dto, queryWrapper, this::page);
        PagerResult<TokenVo> voPagerResult = tokenTransfer.pagePo2PageVo(entityPagerResult);
        return voPagerResult;
    }

    @Override
    public TokenEntity addOrUpdate(TokenDto dto) {
            TokenEntity entity = new TokenEntity();
        BeanUtils.copyProperties(dto, entity);
        this.saveOrUpdate(entity);
        return entity;
    }

    @Override
    public List<TokenEntity> addOrUpdateList(List<TokenDto> dto) {
        List<TokenEntity> entitys = tokenTransfer.listDto2listPo(dto);
        this.saveOrUpdateBatch(entitys);
        return entitys;
    }

    @Override
    public List<TokenVo> listFor(TokenQueryDto dto) {
        QueryWrapper<TokenEntity> queryWrapper = PageCommonUtil.dtoToWrapper(TokenEntity.class, dto);
        List<TokenEntity> list = this.list(queryWrapper);
        List<TokenVo> vos = tokenTransfer.listPo2listVo(list);
        return vos;
    }

    @Override
    public TokenVo detail(Long id) {
            TokenEntity entity = this.getById(id);
            TokenVo vo = new TokenVo();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    @Override
    public TokenEntity createToken(Long userId) {
        //当前时间
        LocalDateTime nowDate = LocalDateTime.now();
        //过期时间
        LocalDateTime expireDateTime = nowDate.plusDays(EXPIRE);

        //用户token
        String token;

        //判断是否生成过token
        TokenEntity tokenEntity = this.getById(userId);
        if(tokenEntity == null){
            //生成一个token
            token = generateToken();

            tokenEntity = new TokenEntity();
            tokenEntity.setUserId(userId);
            tokenEntity.setToken(token);
            tokenEntity.setUpdateDate(nowDate);
            tokenEntity.setExpireDate(expireDateTime);

            //保存token
            this.save(tokenEntity);
        }else{
            token = generateToken();
            tokenEntity.setToken(token);
            tokenEntity.setUpdateDate(nowDate);
            tokenEntity.setExpireDate(expireDateTime);

            //更新token
            this.updateById(tokenEntity);
        }

        return tokenEntity;
    }

    @Override
    public TokenEntity getByToken(String token) {
        return this.getOne(new LambdaQueryWrapper<TokenEntity>()
                .eq(TokenEntity::getToken, token));
    }

    private String generateToken(){
        return UUID.randomUUID().toString().replace("-", "");
    }
}



